﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Miziak_Custom_Client_Symulator
{
    public partial class Form1 : Form
    {
        const int ip_distance = 112;
        const int port_distance = 100;

        public Form1()
        {
            InitializeComponent();
            this.ControlBox = false;
            this.Text = string.Empty;
            this.TopMost = true;
            c = new ConfigLoader();
            if (!File.Exists("data/" + c.background))
            {
                MessageBox.Show("Background image file not found!", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Shown += new EventHandler(Form1_CloseOnStart);
            }
            else if (!File.Exists("data/" + c.start.image))
            {
                MessageBox.Show("Start button image file not found!", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Shown += new EventHandler(Form1_CloseOnStart);
            }
            else if (!File.Exists("data/" + c.exit.image))
            {
                MessageBox.Show("Exit button image file not found!", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Shown += new EventHandler(Form1_CloseOnStart);
            }
            else if (!File.Exists("Tibia.exe"))
            {
                MessageBox.Show("Can not find Tibia executable file!", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Shown += new EventHandler(Form1_CloseOnStart);
            }
            else
            {
                //Images
                Image background = Image.FromFile("data/" + c.background);
                Image btn_start = Image.FromFile("data/" + c.start.image);
                Image btn_exit = Image.FromFile("data/" + c.exit.image);

                //Frame
                this.MinimumSize = background.Size;
                this.MaximumSize = background.Size;
                this.Size = background.Size;

                //Background
                pictureBox1.Size = background.Size;
                pictureBox1.Image = background;

                //Start button
                pictureBox2.Size = btn_start.Size;
                pictureBox2.Image = btn_start;
                pictureBox2.Location = c.start.pos;

                //Exit button
                pictureBox3.Size = btn_exit.Size;
                pictureBox3.Image = btn_exit;
                pictureBox3.Location = c.exit.pos;

                mem = new Memory();

                timer = new Timer();
                timer.Interval = 1000;
                timer.Tick += new System.EventHandler(OnTimerEvent);
                timer.Enabled = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e) {}

        private void Form1_CloseOnStart(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Tibia.exe");
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OnTimerEvent(object sender, EventArgs e)
        {
            if (mem.checkTibia())
            {
                mem.setTitlebar(c.name);

                ConfigLoader.Protocol version = c.versions[c.protocol];

                if (version.rsa != 0x00)
                    mem.writeRSA(version.rsa);

                for (int i = 0; i < version.server_count; i++)
                {
                    long tmp_addr = version.ip + (ip_distance * i);
                    mem.WriteString(tmp_addr, c.IP);
                    mem.WriteInt32(tmp_addr + port_distance, c.port);
                }

                if (sec <= 0)
                    this.Close();

                sec--;
            }
        }

        private ConfigLoader c;
        private Memory mem;
        static Timer timer;
        private int sec = 2;
    }
}
